<template>
  <div>
    <!--     搜索条件-->
    <SearchForm @search="getTableData" :isSearchDisabled="isSearchDisabled" @reset="resetPage">
      <el-col :span="8" :xs="24" v-show="$permiss[201]">
        <el-form-item :label="$t('所属租户')" prop="adminUserId">
          <el-select style="width: 100%" @change="tenantChange" v-model="page.adminUserId" clearable>
            <el-option v-for="item in tenantList" :key="item.adminUserId" :label="item.adminName"
                       :value="item.adminUserId"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('订单编号')" prop="loanRecordId">
          <el-input v-model="page.loanRecordId"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('用户姓名')" prop="name">
          <el-input v-model="page.name"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('手机号码')" prop="phone">
          <el-input v-model="page.phone"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('身份证')">
          <el-input v-model="page.idcard" clearable></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24" v-show="$permiss[202]">
        <el-form-item :label="$t('APP名称')" prop="productName">
          <el-select style="width: 100%" v-model="page.productName" clearable>
            <el-option v-for="item in productList" :key="item.productId" :label="item.appName"
                       :value="item.productName"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('还款流水')" prop="transferNumber">
          <el-input v-model="page.transferNumber"></el-input>
        </el-form-item>
      </el-col>

      <el-col :span="12" :xs="24">
        <el-form-item :label="$t('还款时间')" prop="repaymentTime">
          <el-date-picker
              v-model="page.repaymentTime"
              style="width: 100%"
              type="daterange"
              unlink-panels
              range-separator="~"
              value-format="yyyy-MM-dd"
              :start-placeholder="$t('开始时间')"
              :end-placeholder="$t('结束时间')">
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="12" :xs="24">
        <el-form-item :label="$t('操作时间')" prop="rollOverTime">
          <el-date-picker
              v-model="page.rollOverTime"
              style="width: 100%"
              type="daterange"
              unlink-panels
              range-separator="~"
              value-format="yyyy-MM-dd"
              :start-placeholder="$t('开始时间')"
              :end-placeholder="$t('结束时间')">
          </el-date-picker>
        </el-form-item>
      </el-col>
    </SearchForm>


    <el-card shadow="never" style="margin-top: 10px">
      <div style="text-align: right;margin-bottom: 10px" v-show="$permiss[782]">
        <el-button type="primary" @click="exportsExcel" :loading="exportsLoading">{{ $t('导出') }}</el-button>
      </div>
      <Table :page="page" :loading="loading" :table-column="tableColumn" :table-data="tableData"
             @handleSizeChange="handleSizeChange"
             @handleCurrentChange="handleCurrentChange">
        <template slot="status" slot-scope="{row}">
          {{ row.status === 21 ? $t('成功') : row.status === 12 ? $t('失败') : '' }}
        </template>
        <template slot-scope="{row}" slot="certificate">
          <el-image v-if="row.credentials" style="height: 80px;" :src="row.credentials"
                    :preview-src-list="[row.credentials]"></el-image>
        </template>
      </Table>
    </el-card>
  </div>
</template>

<script>
import {export_json_to_excel} from "@/utils/exportExcel";
import {dateFormatSection, defaultTimeDate} from '@/utils/tools'
import {selectTenant, selectProduct, selectAuditStaff} from "@/api/system"
import {rollOverBillsExport, selectRollOverBills} from '@/api/finance'

export default {
  name: 'exhibitionDetails',
  data() {
    return {
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        rollOverTime: defaultTimeDate(14)
      },
      tableData: [],
      tableColumn: [
        {label: this.$t('订单编号'), prop: 'loanRecordId', fixed: 'left', align: 'center', minWidth: 150},
        {label: this.$t('用户姓名'), prop: 'name', align: 'center', minWidth: 150},
        {label: this.$t('手机号码'), prop: 'phone', align: 'center', minWidth: 100},
        {label: this.$t("身份证"), prop: "idCard", align: "center", minWidth: 100,},
        {label: this.$t('展期金额'), prop: 'rollOverAmount', align: 'center',},
        {label: this.$t('原还款时间'), prop: 'originalRefundTime', align: 'center', minWidth: 150},
        {label: this.$t('展期天数'), prop: 'rollOverDay', align: 'center',},
        {label: this.$t('展期后还款时间'), prop: 'nowRefundTime', align: 'center', minWidth: 150},
        {label: this.$t('展期利率'), prop: 'delayFee', align: 'center',},
        {label: this.$t('展期手续费'), prop: 'delayAmount', align: 'center', minWidth: 150},
        {label: this.$t('展期状态'), prop: 'status', slot: 'status', align: 'center',},
        {label: this.$t('交易流水'), prop: 'transferNumber', align: 'center', minWidth: 200},
        {label: this.$t('展期次数'), prop: 'rollOverNum', align: 'center',},
        {label: this.$t('操作人'), prop: 'inputName', align: 'center',},
        {label: this.$t('操作时间'), prop: 'rollOverTime', align: 'center', minWidth: 150},
        {label: this.$t('APP名称'), prop: 'appName', align: 'center',},
        {label: this.$t('凭证'), align: 'center', slot: 'certificate', prop: 'certificate', minWidth: 150},
        {label: this.$t('备注'), prop: 'remark', align: 'center', minWidth: 200},
        {label: this.$t('所属租户'), prop: 'adminUserName', align: 'center', isHide: !this.$permiss[201]},
      ],
      loading: false,
      tenantList: [],
      productList: [],
      exportsLoading: false,
      isSearchDisabled: false
    }
  },
  mounted() {
    this.getTableData()
    this.getSelectList()
  },
  methods: {
    async getTableData() {
      this.loading = true
      this.isSearchDisabled = true
      const [refundTimeStart, refundTimeEnd] = dateFormatSection(this.page.repaymentTime)
      const [rollOverStart, rollOverEnd] = dateFormatSection(this.page.rollOverTime)
      const params = {
        refundTimeStart, refundTimeEnd, rollOverStart, rollOverEnd,
        ...this.page
      }
      // 删除日期属性
      delete params.repaymentTime
      delete params.rollOverTime
      const res = await selectRollOverBills(params)
      if (res.code === 1) {
        const {list, total} = res.data
        this.tableData = list.map(item => {
          item.avoidAudit = item.avoidAudit === 1 ? this.$t('是') : this.$t('否')
          item.delayFee = item.delayFee + '%'
          return item
        })
        this.page.total = total
        this.isSearchDisabled = false
        this.loading = false
      } else {
        this.isSearchDisabled = false
        this.loading = false
      }
    },
    handleCurrentChange(val) {
      this.page.pageNum = val
      this.getTableData()
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getTableData()
    },
    async getSelectList() {
      selectTenant().then(res => {
        this.tenantList = res.data
      })
      selectProduct().then(res => {
        this.productList = res.data
      })
    },
    // 导出excel
    async exportsExcel() {
      this.exportsLoading = true;
      const [rollOverStart, rollOverEnd] = dateFormatSection(this.page.rollOverTime || [])
      const [refundTimeStart, refundTimeEnd] = dateFormatSection(this.page.repaymentTime || [])
      const params = {
        ...this.page,
        rollOverStart, rollOverEnd,
        refundTimeStart, refundTimeEnd
      };
      delete params.repaymentTime;
      delete params.rollOverTime;
      const res = await rollOverBillsExport(params);
      if (res.code === 1) {
        const headerData = [...this.tableColumn].filter(item => {
          const result = res.data.filter(i => {
            //过滤没有权限的字段
            return typeof i[item.prop] !== "undefined"
          })
          return result.length !== 0
        })
        const column = headerData.map(item => item.label)
        const filterVal = headerData.map(item => item.prop)
        const list = [...res.data].map((item) => {
          item.avoidAudit = item.avoidAudit === 1 ? this.$t('是') : this.$t('否')
          item.delayFee = item.delayFee + '%'
          item.status = item.status === 21 ? this.$t('成功') : item.status === 12 ? this.$t('失败') : ''
          return item
        }).map((v) => filterVal.map((j) => v[j]));
        const fileName = this.$t("展期记录明细");
        export_json_to_excel({
          header: column,
          data: list,
          filename: fileName,
        });
        this.exportsLoading = false;
      } else {
        this.exportsLoading = false;
      }
    },
    tenantChange(value) {
      selectProduct({adminUserId: value}).then(res => {
        this.productList = res.data
        this.$set(this.page,'productName', null)
      })
    },
    resetPage() {
      this.page = {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      }
      this.getTableData()
    }
  }
}
</script>

<style scoped>

</style>
